<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 抢红包ajax类
 * @package  ajax
 * @subpackage  shop
 * @author   allen.qiang
 */

quanli_require('module/base/customer/quanliCust.class.php');
quanli_require('module/base/promotion/quanliCoupon.class.php');

class ajax_redPacket {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //开始抢红包
                    $this->start();
                    break;
                case 2: //查询我的红包
                    $this->getCoupons();
                    break;
                case 3: //获取时间
                    $this->getDate();
                    break;
                default:
                    returnJsonData('参数错误');
                    break;


            }

        } else {
            returnJsonData('参数错误');
        }

    }
    private function getDate()
    {
        returnJsonData(date('Y-m-d H:i:s'),200,null);
    }

    private function getCoupons()
    {
        global $G_SHOP;
        //实例优惠券类
        $promotion_coupon_obj = new quanliCoupon();
        $arg_array = array(
            'pcustID' => $G_SHOP->currentPCustID,
            'useType' => 2,
            'sortname'=>'createTime',
            'sortorder'=>'desc'
        );
        //查询会员信息
        $coupons = $promotion_coupon_obj->searchCouponCode( $arg_array );
        //error_log(print_r($coupons,1));
        returnJsonData( '我的红包列表', 200, $coupons);
    }


    private function start() {

        global $G_SHOP;

        if (!$G_SHOP->currentPCustID) {
            returnJsonData('会员未登录', 1);
        }

        //实例会员类
        $quanliCust = new quanliCust();

        //实例优惠券类
        $ebsigCoupon = new quanliCoupon();

        //查询会员信息
        $customer_array = $quanliCust->searchCustomerDetail( array( 'pcustID' => $G_SHOP->currentPCustID ) );
        if (!$customer_array) {
            returnJsonData('会员未登录', 1);
        }
        if ($customer_array['rankID'] == 1) {
            returnJsonData('等级未到LV2，还不能抽取红包', 2);
        }

        $date = date('Y-m-d');
        //检查活动时间
        $time = date('His');
        if(date('w')!=2)
        {
            returnJsonData( '活动未开始', 6 );
        }


        if($time >= 130000 && $time < 140000)
        {
            returnJsonData( '活动未开始', 6 );
        }

        if ($time < 100000) {
            returnJsonData( '活动未开始', 6 );
        }
        else if ($time >= 100000 && $time < 140000) {
            $index = 1;
        } else {
            $index = 2;
        }

        //检查是否已参与
        //$get_log_obj =& $G_SHOP->DBCA->stmt_query('SELECT pcustID FROM promotion_coupon_get_log WHERE pcustID = ' . $G_SHOP->currentPCustID);

        $sql='SELECT pcustID FROM promotion_coupon_get_log WHERE pcustID = ? and activity_date = ? and activity_round = ?';
        $get_log_obj =& $G_SHOP->DBCA->stmt_query($sql,'isi',array($G_SHOP->currentPCustID,$date,$index));

        if ($get_log_obj) {
            returnJsonData('亲已在本场抢过红包了哟~', 4);
        }





        //红包数组
        $coupon_array = array(
            1 => 794,  //1元
            3 => 795,   //2元
            4 => 796,   //3元
            5 => 0,   //100元
            6 => 797    //5元
        );

        //红包数量
        $coupon_array_count = array(
            794=>30,
            798=>1,
            795=>15,
            796=>2,
            797=>2
        );

        //本场红包数量
        $total_redPacket = 0;
        $d_date = date('d');
        if($index==2 && ($d_date-7)<=0) //每月第一个周二
        {
            //红包数组
            $coupon_array = array(
                1 => 794,  //1元
                2 => 798,   //10元
                3 => 795,   //2元
                4 => 796,   //3元
                6 => 797    //5元
            );

            $total_redPacket = 50;//50个红包
        }
        else
        {
            //优惠券数组
            $coupon_array = array(
                1 => 794,  //1元
                3 => 795,   //2元
                4 => 796,   //3元
                6 => 797    //5元
            );
            $total_redPacket = 49;
        }


        //本场已抢的红包记录
        $stringSql = "SELECT * FROM promotion_coupon_get_log where activity_date = ? and activity_round = ?";
        $qiang_redPacket =& $G_SHOP->DBCA->stmt_query($stringSql,"si",array($date,$index));

        if($qiang_redPacket)
        {
            if( count($qiang_redPacket) >= $total_redPacket)
            {
                returnJsonData('红包已抢完下场努力抢哦', 5);
            }
        }


        while(1)
        {
            //随机数组

            sleep(1);
            if(count($coupon_array)>0)
            {
                $key = array_rand( $coupon_array );
                $couponID = $coupon_array[$key];

                //returnJsonData("抢到:"+$couponID, 5);
                //判断该红包是否被抢光
                $stringSql = "SELECT count(*) qiang_num FROM promotion_coupon_get_log where activity_date = ? and activity_round = ? and couponID = ?";

                $qiang_num =& $G_SHOP->DBCA->stmt_query($stringSql,"sii",array($date,$index,$couponID));
                if($coupon_array_count[$couponID]>$qiang_num[0]->qiang_num)
                {


                    $log_obj = new entity('promotion_coupon_get_log');
                    $log_obj->creator = 'system';
                    $log_obj->pcustID = $G_SHOP->currentPCustID;
                    $log_obj->couponID = $couponID;
                    $log_obj->activity_date = $date;
                    $log_obj->activity_round = $index;
                    $G_SHOP->DBCA->save( $log_obj );


                    $result_array = $ebsigCoupon->createCouponCode( $couponID, array( $G_SHOP->currentCustomerID ) );
                    if($result_array['code']!=200)
                    {
                        unset($coupon_array[$key]);
                        continue ;
                    }

                    break;
                }
                else
                {
                    unset($coupon_array[$key]);
                    continue ;
                }

            }
            else
            {
                returnJsonData('红包已抢完下场努力抢哦', 5);
            }

        }






        //error_log(print_r($key,1));

        //循环生成随机数，并得到优惠券ID，检查优惠券是否已发放完毕
//        $i = 1;
//        $num = 0;
//        while($i) {
//
//            //随机数
//            $num = array_rand( $range_array );
//
//            if($num==5)
//            {
//                $num=1;
//            }
//
//            $couponID = $coupon_array[$index][$num];
//            $result_array = $ebsigCoupon->createCouponCode( $couponID, array( $G_SHOP->currentCustomerID ) );
//            if ($result_array['code'] == 200) {
//                $i = 0;
//                $log_obj = new entity('promotion_coupon_get_log');
//                $log_obj->creator = 'system';
//                $log_obj->pcustID = $G_SHOP->currentPCustID;
//                $log_obj->couponID = $couponID;
//                $log_obj->activity_date = $date;
//                $log_obj->activity_round = $index;
//                $G_SHOP->DBCA->save( $log_obj );
//            } else {
//                unset($range_array[array_search($num , $range_array)]);
//                $num = 0;
//            }
//
//        }

//        if ($num == 0) {
//            returnJsonData('红包已抢完下场努力抢哦', 5);
//        }

        returnJsonData( 'ok', 200, array( 'num' => $key ) );

    }

}